package com.ujoin.dao.imp;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.Broadcast;
import com.ujoin.dao.IBroadcastDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Broadcast entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.Broadcast
 * @author MyEclipse Persistence Tools
 */

public class BroadcastDAO extends HibernateDaoSupport implements IBroadcastDAO {
	private static final Logger log = LoggerFactory
			.getLogger(BroadcastDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String ACTICITY_ID = "activityId";
	public static final String IS_READ = "isRead";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#save(com.ujoin.bean.Broadcast)
	 */
	@Override
	public void save(Broadcast transientInstance) {
		log.debug("saving Broadcast instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#delete(com.ujoin.bean.Broadcast)
	 */
	@Override
	public void delete(Broadcast persistentInstance) {
		log.debug("deleting Broadcast instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findById(java.lang.Integer)
	 */
	@Override
	public Broadcast findById(java.lang.Integer id) {
		log.debug("getting Broadcast instance with id: " + id);
		try {
			Broadcast instance = (Broadcast) getHibernateTemplate().get(
					"com.ujoin.bean.Broadcast", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findByExample(com.ujoin.bean.Broadcast)
	 */
	@Override
	public List findByExample(Broadcast instance) {
		log.debug("finding Broadcast instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Broadcast instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Broadcast as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findByUserId(java.lang.Object)
	 */
	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findByActicityId(java.lang.Object)
	 */
	@Override
	public List findByActicityId(Object activityId) {
		return findByProperty(ACTICITY_ID, activityId);
	}
	
	

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findByIsRead(java.lang.Object)
	 */
	@Override
	public List findByIsRead(Object isRead) {
		return findByProperty(IS_READ, isRead);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Broadcast instances");
		try {
			String queryString = "from Broadcast";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#merge(com.ujoin.bean.Broadcast)
	 */
	@Override
	public Broadcast merge(Broadcast detachedInstance) {
		log.debug("merging Broadcast instance");
		try {
			Broadcast result = (Broadcast) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#attachDirty(com.ujoin.bean.Broadcast)
	 */
	@Override
	public void attachDirty(Broadcast instance) {
		log.debug("attaching dirty Broadcast instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IBroadcastDAO#attachClean(com.ujoin.bean.Broadcast)
	 */
	@Override
	public void attachClean(Broadcast instance) {
		log.debug("attaching clean Broadcast instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IBroadcastDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IBroadcastDAO) ctx.getBean("BroadcastDAO");
	}
	
	/**
	 * author Daniel
	 * 待测试
	 */
	public Broadcast findByUserIdActivityId(Object userId, Object activityId) {
		log.debug("fingding all Broadcast instances with certain (user,activity) pair");
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Query query = session.createQuery("from Broadcast b where b.userId = :u and b.activityId = :a");
			query.setParameter("u", userId);
			query.setParameter("a", activityId);
			List broadcasts = query.list();
			return (Broadcast)broadcasts.get(0);
		} catch (RuntimeException re) {
			log.error("find by userId and activityId failed", re);
			throw re;
		}
	}
	
	public List findUnreadBroadcastByUserId(Object userId) {
		log.debug("finding all Unread Broadcast instances of a certain user");
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Query query = session.createQuery("from Broadcast b where b.userId = :u and b.isRead = :r");
			query.setParameter("u", userId);
			query.setParameter("r", 0);
			List broadcasts = query.list();
			return broadcasts;
		} catch (RuntimeException re) {
			log.error("find unread broadcasts failed", re);
			throw re;
		}
	}
}